#include <iostream>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <sms_core.h>


int main() {
    const char* name = "/_My_Shm";  // 共享内存对象名称
    const int size = 1024*1024*60;         // 共享内存大小

    // 打开共享内存对象
    int shm_fd = shm_open(name, O_RDONLY, 0666);
    if (shm_fd == -1) {
        std::cerr << "Failed to open shared memory object" << std::endl;
        return 1;
    }

    // 映射共享内存到进程地址空间
    char* ptr = (char*)mmap(0, size, PROT_READ, MAP_SHARED, shm_fd, 0);
    if (ptr == MAP_FAILED) {
        std::cerr << "Failed to map shared memory" << std::endl;
        return 1;
    }

    // 读取共享内存中的数据
    while (1)
    {
    double t1 = sms::get_time_sec();
    cv::Mat img(2160, 3840, CV_8UC3, ptr);
    cv::Mat img2;
    img.copyTo(img2);
    std::cout << sms::get_time_sec() - t1 << std::endl;
    cv::imshow("img", img2);
    cv::waitKey(5);
    }
    // std::cout << "Data read from shared memory: " << ptr << std::endl;

    // 解除映射
    munmap(ptr, size);

    // 关闭共享内存对象
    close(shm_fd);

    return 0;
}

